import React from 'react';
import {Button, Icon, message} from 'antd';
import {WeaTools, WeaLocaleProvider, WeaDialog, WeaFormItem, WeaInput, WeaUpload} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

import './style/';
import Message from './Message';

class WeaLicenseSubmit extends React.Component {
    constructor(props) {
        super(props);
        this.getButtons = this.getButtons.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
        this.onCodeSubmit = this.onCodeSubmit.bind(this);
        this.onBack = this.onBack.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.onShow = this.onShow.bind(this);

        this.state = {from: props.from, visible: props.visible || false, loading: false, licenseCode: '', data: {}};

        if (props.visible) {
            this.onShow();
        }
    }

    render() {
        const {visible, loading, licenseCode, data} = this.state;
        const {code = '', code_old = '', code_new = '', code_rep = ''} = data;

        const colProps = {labelCol: {span: 6}, wrapperCol: {span: 18}};

        let displayComp = (
            <div className="wea-license-form-group">
                <WeaFormItem label={getLabel(22910, '验证码')} {...colProps}>
                    <WeaInput style={{width: '270px'}} type="password" value={code} viewAttr={3} onChange={value => this.setState({data: {...data, code: value}})}/>
                    <a style={{marginLeft: '20px'}} href="javascript:;" onClick={() => this.setState({isChangeCode: true})}>{getLabel(293, '更改')+getLabel(22910, '验证码')}</a>
                </WeaFormItem>
                <WeaFormItem label={'license'+getLabel(18493, '文件')} {...colProps}>
                    <WeaUpload
                        ref="WeaUpload"
                        uploadId="LicenseSubmit"
                        uploadUrl="/api/system/license/LicenseOperation"
                        uploadParams={{code: code}}
                        category="string"
                        clearWhenReset={false}
                        multiSelection={false}
                        maxFilesNumber={1}
                        showClearAll={false}
                        autoUpload={false}
                        limitType="license"
                        viewAttr={3}
                        onChange={(ids, list, args) => this.setState({isShowMsg: true, message: args[0].message})}
                    />
                </WeaFormItem>
                <WeaFormItem label={getLabel(15019, '识别码')} {...colProps} underline>
                    <WeaInput value={licenseCode} viewAttr={1}/>
                </WeaFormItem>
                <div className="wea-license-tip-msg">
                    <Icon type="exclamation-circle"/>
                    <span style={{marginLeft: '10px'}}>{getLabel(82060, '提示：')+getLabel(1976, '公司名称')+getLabel(15022, '及')+getLabel(15019, '识别码')+getLabel(15023, '提交给软件供应商，以获取license。')}</span>
                </div>
            </div>
        );

        if (this.state.isShowMsg) {
            displayComp = <Message message={this.state.message}/>;
        }

        if (this.state.isChangeCode) {
            displayComp = (
                <div className="wea-license-form-group">
                    <WeaFormItem label={getLabel(502, '旧')+getLabel(22910, '验证码')} {...colProps}>
                        <WeaInput type="password" value={code_old} viewAttr={3} onChange={value => this.setState({data: {...data, code_old: value}})}/>
                    </WeaFormItem>
                    <WeaFormItem label={getLabel(22910, '验证码')} {...colProps}>
                        <WeaInput type="password" value={code_new} viewAttr={3} onChange={value => this.setState({data: {...data, code_new: value}})}/>
                    </WeaFormItem>
                    <WeaFormItem label={getLabel(16631, '确认')+getLabel(22910, '验证码')} {...colProps}>
                        <WeaInput type="password" value={code_rep} viewAttr={3} onChange={value => this.setState({data: {...data, code_rep: value}})}/>
                    </WeaFormItem>
                </div>
            );
        }

        return (
            <WeaDialog
                visible={visible}
                loading={loading}
                title={'License'+getLabel(615, '提交')}
                icon="wevicon-wea-license"
                iconBgcolor='#a7adb5'
                style={{width: 600}}
                zIndex={999}
                hasScroll={true}
                buttons={this.getButtons()}
                onCancel={this.onCancel}
            >
                {displayComp}
            </WeaDialog>
        );
    }

    getButtons() {
        const {from, isShowMsg, message, isChangeCode} = this.state;

        let buttons = [];
        if (isChangeCode) {
            buttons.push(<Button type="primary" onClick={this.onCodeSubmit}>{getLabel(615, '提交')}</Button>);
            buttons.push(<Button type="primary" onClick={this.onBack}>{getLabel(31129, '取消')}</Button>);
        } else {
            if (isShowMsg) {
                if (message == '1') {
                    buttons.push(<Button type="primary" onClick={this.onCancel}>{from == 'login' ? getLabel(674, '登录') : getLabel(309, '关闭')}</Button>);
                } else {
                    buttons.push(<Button type="primary" onClick={this.onBack}>{getLabel(1290, '返回')}</Button>);
                }
            } else {
                buttons.push(<Button type="primary" onClick={this.onSubmit}>{getLabel(615, '提交')}</Button>);
            }
        }
        return buttons;
    }

    onSubmit() {
        const {data} = this.state;
        const {code = ''} = data;
        if (code.trim() != '') {
            this.refs['WeaUpload'].doUpload();
        } else {
            message.warning(getLabel(384134, '必要信息不完整！'));
        }
    }

    onCodeSubmit() {
        const {data} = this.state;
        const {code_old = '', code_new = '', code_rep = ''} = data;

        if (code_old.trim() != '' && code_new.trim() != '' && code_rep.trim() != '') {
            if (code_new === code_rep) {
                this.setState({loading: true});
                WeaTools.callApi('/api/system/license/CodeOperation', 'POST', {passwordold: code_old, passwordnew: code_new}).then(data => {
                    const msg = data.message;
                    if (msg == '1') {
                        message.error(getLabel(128878, '验证码有误'));
                    } else {
                        message.success(msg);
                        this.onBack();
                    }
                    this.setState({loading: false});
                });
            } else {
                message.warning(getLabel(128878, '验证码有误'));
            }
        } else {
            message.warning(getLabel(384134, '必要信息不完整！'));
        }
    }

    onBack() {
        this.setState({data: {}, isShowMsg: false, isChangeCode: false});
    }

    onCancel() {
        this.setState({visible: false, data: {}, isShowMsg: false, isChangeCode: false});
    }

    onShow() {
        this.setState({visible: true, loading: true});
        WeaTools.callApi('/api/system/license/InLicense', 'POST', {}).then(data => {
            this.setState({loading: false, licenseCode: data.licenseCode});
        });
    }
}

export default WeaLicenseSubmit;
